package com.net.xpay.openapi.service.impl;

import com.net.common.util.MoneyUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.core.domain.ChargeRecord;
import com.net.xpay.core.model.ChargeModel;
import com.net.xpay.core.service.ChargeService;
import com.net.xpay.openapi.model.OpenApiChargeRecordChargeModel;
import com.net.xpay.openapi.service.OpenApiChargeRecordService;
import com.net.xpay.openapi.validator.OpenApiChargeRecordValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OpenApiChargeRecordServiceImpl implements OpenApiChargeRecordService {


    @Autowired
    private ChargeService chargeService;

    @Autowired
    private OpenApiChargeRecordValidator openApiChargeRecordValidator;


    @Override
    public String charge(User user, OpenApiChargeRecordChargeModel model) {
        openApiChargeRecordValidator.ensureChargeParameter(user, model);

        Long amount = MoneyUtil.conventStringToLong(model.getAmount());

        ChargeModel chargeModel = new ChargeModel();
        chargeModel.setImportBizNo(model.getImportBizNo());
        chargeModel.setAmount(amount);
        chargeModel.setPayChannelMoney(amount);
        chargeModel.setNotifyUrl(model.getNotifyUrl());
        chargeModel.setReturnUrl(model.getReturnUrl());
        chargeModel.setExpireMinutes(model.getExpireMinutes());
        chargeModel.setBillPayMethod(model.getBillPayMethod());

        ChargeRecord record = chargeService.doCharge(user, chargeModel);
        return record.getLastPayUrl();
    }
}
